package tt.com.datamanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.CremateStove;
import tt.com.common.model.Team;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.service.TeamService;

@RestController
@RequestMapping(value = "team/manager")
public class TeamController {

    @Autowired
    private TeamService teamService;;

    @PostMapping(value = "show/page")
    public CommonResult showAllTeamByPage(@RequestBody PageUtil pageUtil){
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null){
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = teamService.selectAllTeamByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "所有班组查询成功", pageUtil);
    }

    @PostMapping(value = "update")
    public CommonResult updateTeam(@RequestBody Team team){
        if (team == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (teamService.updateTeam(team, "1") > 0) {
            return CommonResult.buildR(Status.OK, "班组修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "班组修改失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult deleteTeam(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = teamService.deleteTeam(id,"1");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "班组不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "班组已删除");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "班组删除成功");
        }
    }

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody Team team) {
        if (!ObjectUtil.objectIsNull(team)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = teamService.insertTeam(team);
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "班组添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "班组添加失败");
        }
    }

}
